package com.midea.ai.air.common;

import android.app.Application;
import android.content.Context;
import android.os.Build;

import com.midea.ai.air.data.login.UserInfo;
import com.midea.msmartsdk.openapi.MSmartSDK;

/**
 * Created by June Kwok on 2015/12/7.
 */
public class AirApplication extends Application {
    private static AirApplication mApp;

    private UserInfo userInfo;


    public static AirApplication getInstance() {
        return mApp;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (null == getInstance()) {
            mApp = this;
        }
        initSLK();
        Constant.initData(this, Build.VERSION.SDK_INT >= 19);
    }

    /**
     * 初始化SLK相关
     */
    private void initSLK() {
        String appId = "1016";
        String appKey = "8ae7d803224a4de68d28990ccffa8ef5";
        String appSrc = "16";
        MSmartSDK.getInstance().initSDKWithAppID(this.getApplicationContext(), appId, appKey, appSrc);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        mApp = null;
    }

    public static Context getAppContext() {
        return mApp;
    }

    public static AirApplication getApp() {
        return mApp;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    /**
     * 获取用户Token
     *
     * @return
     */
    public String getToken() {
        if (getUserInfo() != null && getUserInfo().getToken() != null) {
            return getUserInfo().getToken();
        }
        return "";
    }

    /**
     * 获取用户id
     *
     * @return
     */
    public String getUid() {
        if (getUserInfo() != null && getUserInfo().getUid() != null) {
            return getUserInfo().getUid();
        }
        return "";
    }

    /**
     * 登出
     */
    public void logout() {
        setUserInfo(null);
    }

    /**
     * 是否登录
     *
     * @return
     */
    public boolean isLogin() {
        return getUserInfo() != null && getUserInfo().getToken() != null;
    }
}
